﻿using System;

namespace Program18
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 冒泡排序
            Console.WriteLine("冒泡排序");
            int temp = 0;
            int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7,11 };
            Console.WriteLine("排序前的数组：");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            for (int i = 0; i < arr.Length; i++)
            {

                for (int j = 0; j < arr.Length -1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                    }
                }

            }
            Console.WriteLine("排序后的数组：");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            #endregion

            #region 选择排序
            Console.WriteLine();
            Console.WriteLine("选择排序");
            Console.WriteLine("排序前的数组：");
            int[] group = { 12, 5, 21, 54, 36, 252 };
            foreach (int item in group)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.WriteLine("排序后的数组：");
            Sort(group);
            Console.WriteLine();
            #endregion

            #region 插入排序
            Console.WriteLine();
            Console.WriteLine("插入排序");
            Program shell = new Program();
            Console.WriteLine("排序前的数组:");
            shell.foreachArray();
            Console.WriteLine("排序后的数组：");
            shell.shellSort1();
            #endregion
        }
        //选择排序
        static void Sort(int[] group)
        {
            int temp;
            int pos = 0;
            for (int i = 0; i < group.Length - 1; i++)
            {
                pos = i;
                for (int j = i + 1; j < group.Length; j++)
                {
                    if (group[j] < group[pos])
                    {
                        pos = j;
                    }
                }//第i个数与最小的数group[pos]交换
                temp = group[i];
                group[i] = group[pos];
                group[pos] = temp;
            }
            foreach (int item in group)
            {
                Console.Write(item + " ");
            }
        }

        int e;
        int times, count;
        int[] a = { 15, 13, 12, 18, 55, 5, 3, 2, 1 };
        //插入排序
        public void shellSort1()
        {
            for (int i = 1; i < a.Length; ++i)
            {
                int t = a[i];
                int j = i;
                while ((j > 0) && a[j - 1] > t)
                {
                    times++;
                    a[j] = a[j - 1];
                    //foreachArray();
                    --j;
                }
                a[j] = t;
                foreachArray();
                count += times;
                times = 0;
            }                      
        }

        public void foreachArray()
        {
            
            for (e = 0; e < a.Length; e++)
            {              
                Console.Write($"{a[e]}\t");
                if (e == a.Length - 1)
                {
                    Console.WriteLine();
                }
            }
            
        }
    }
}
